package com.future.controller;

import javax.validation.constraints.NotBlank;

import com.future.common.annotation.ControllerMapping;
import com.future.common.annotation.NoNeedLogin;
import com.future.common.base.BaseController;
import com.future.common.exception.AppException;
import com.future.dao.BjDao;
import com.future.dao.PreRegisterDao;
import com.future.model.Bj;
import com.future.model.Preregister;
import com.future.requestbean.PreRegisterAdd;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.tx.Tx;

@ControllerMapping("/perregister")
public class PreRegisterController extends BaseController {
	@Inject
	private PreRegisterDao preRegisterDao;
	@Inject
	private BjDao bjDao;

	@NoNeedLogin
	@Before(Tx.class)
	public void addPreregister() {
		PreRegisterAdd preRegisterAdd = getJsonBody(PreRegisterAdd.class);
		Preregister preregister = beanToModel(preRegisterAdd, Preregister.class);
		Bj bj = this.bjDao.findById(preRegisterAdd.getBjid());
		Integer ybmrs = bj.getYbmrs() == null ? 0 : bj.getYbmrs();
		Integer rs = bj.getRs() == null ? 0 : bj.getRs();
		if (ybmrs >= rs) {
			throw new AppException("该班级已经满额了,请选择其他班级");
		}
		preregister.save();
		bj.setYbmrs(ybmrs + 1);
		bj.update();
		renderSuccess();
	}

	public void queryPreregister() {
		renderResult("preregisters", this.preRegisterDao.getPreRegisters());
	}

	public void ShPreregister(@NotBlank(message = "选择相应的预报名信息") String ybmid,
			@NotBlank(message = "审核状态不能为空") String state) {
		Preregister preregister = this.preRegisterDao.findById(ybmid);
		preregister.setState(state);
		preregister.update();
		renderSuccess();
	}

	public void getBabys() {
		renderResult("babys", this.preRegisterDao.getBabys());
	}

}
